import time
import webbrowser
from selenium.webdriver.common.by import By
from common.base import Base
from common.common_fn import open_browser
from page.ecshop_index import Index
from page.ecshop_login import Login
import unittest


class shopping(Base):

    #点击购物车
    def click_shoppcat(self):
        time.sleep(2)
        #self.left_click(By.XPATH,'//*[@id="ECS_CARTINFO"]/a')
        self.click((By.XPATH,'//*[@id="ECS_CARTINFO"]/a'))
   # 设置隐式等待
        driver.implicitly_wait(10)
    # 点击继续购物
    def cilck_shopp(self):
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img'))

    # 点击你想点击的商品
    def click_goods(self):
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]/div[1]/img'))

    # 点击购买
    def click_buy(self):
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img'))

    # 清空购物数量
    def clear_user(self):
        time.sleep(2)
        self.clear_text((By.XPATH,'/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[5]/input'))

        # 在点击一下
        self.click((By.XPATH,'/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[5]/input'))

        # 更改数量
        time.sleep(2)
        driver.find_element(By.XPATH,'/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[5]/input').send_keys('5')

    # 点击更新购物车
    def Refresh(self):
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[2]'))
    # 点击删除商品
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="formCart"]/table[1]/tbody/tr[2]/td[7]/a[1]'))

    #获取弹窗
    time.sleep(2)
    def switch(self):
        self.switch_to_alert()

    # 点击清空购物车
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]'))

    # 点击继续购物
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img'))

    # 再次点击商品
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/div/div/div[2]/div[2]/div/a[3]/div[1]/img'))

    # 点击购买
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img'))

    # 定位搜索框
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="keyword"]'))
        driver.find_element(By.XPATH,'//*[@id="keyword"]').send_keys('平衡车')

    #点击搜索
        time.sleep(2)
        self.click((By.XPATH,'//*[@id="searchForm"]/table/tbody/tr/td[2]/input'))

    # 返回浏览器
        time.sleep(2)
        self.browser_back()

    # 点击结算
        time.sleep(2)
        self.click((By.XPATH,'/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a/img'))
        
    # 退出浏览器
    def quit(self, seconds=5):
        time.sleep(seconds)
        driver.quit()

if __name__ == '__main__':
    driver = open_browser()
    shop = shopping(driver)
    login = Login(driver)
    index = Index(driver)
    # 请求网址
    login.get('http://172.16.140.91/ecshop/')
    # denglu
    index.btn_index_login()
    login.ipt_username('zhangsan')
    login.ipt_password('123456')
    login.btn_login()
    shop.click_shoppcat()
    shop.cilck_shopp()
    shop.click_goods()
    shop.click_buy()
    shop.clear_user()
    shop.Refresh()
    shop.switch()
    shop.quit()
















